package com.bmf.module.examination.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bmf.module.examination.entity.FixedTestQuestion;
import com.bmf.module.examination.mapper.FixedTestQuestionMapper;
import com.bmf.module.examination.service.FixedTestQuestionService;
import com.bmf.module.examination.vo.FixedTestQuestionCreateVo;
import com.bmf.module.examination.vo.FixedTestQuestionResultVo;
import com.bmf.module.examination.vo.FixedTestQuestionUpdateVo;
import com.bmf.module.testQuestions.service.SmallQuestionService;
import com.bmf.module.testQuestions.vo.QuestionResultVo;
import com.bmf.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * (FixedTestQuestion)表服务实现类
 *
 * @author makejava
 * @since 2023-01-03 10:12:25
 */
@Service("fixedTestQuestionService")
public class FixedTestQuestionServiceImpl extends ServiceImpl<FixedTestQuestionMapper, FixedTestQuestion> implements FixedTestQuestionService {

    @Autowired
    private SmallQuestionService smallQuestionService;

    @Override
    public void insert(FixedTestQuestionCreateVo fixedTestQuestionCreateVo) {
        List<Integer> smallQuestionIds = fixedTestQuestionCreateVo.getSmallQuestionIds();
        if(CollUtil.isNotEmpty(smallQuestionIds)){
            List<FixedTestQuestion> fixedTestQuestions = smallQuestionIds.stream()
                    .map(s -> new FixedTestQuestion(null, fixedTestQuestionCreateVo.getExaminationId(), fixedTestQuestionCreateVo.getBigQuestionId(), s,null))
                    .collect(Collectors.toList());
            saveBatch(fixedTestQuestions);
        }
    }

    @Override
    public Map<String, List<FixedTestQuestionResultVo>> selectAll(List<Integer> ids) {
        LambdaQueryWrapper<FixedTestQuestion> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(FixedTestQuestion::getBigQuestionId, ids);
        List<FixedTestQuestion> list = list(wrapper);
        if(CollUtil.isEmpty(list)){
            return null;
        }
        List<Integer> questionIds = list.stream().map(FixedTestQuestion::getSmallQuestionId).collect(Collectors.toList());
        List<QuestionResultVo> questionResultVos = smallQuestionService.selectByIds(questionIds);
        if(CollUtil.isNotEmpty(questionResultVos)){
            Map<Integer, QuestionResultVo> collect = questionResultVos.stream().collect(Collectors.toMap(QuestionResultVo::getId, Function.identity()));
            Map<Integer, List<FixedTestQuestion>> map = list.stream().collect(Collectors.groupingBy(FixedTestQuestion::getBigQuestionId));
            return ids.stream().collect(Collectors.toMap(s->s+"", s -> {
                List<FixedTestQuestion> fixedTestQuestions = map.get(s);
                if (CollUtil.isNotEmpty(fixedTestQuestions)) {
                    List<FixedTestQuestionResultVo> fixedTestQuestionResultVos = BeanCopyUtils.copyBeanList(fixedTestQuestions, FixedTestQuestionResultVo.class);
                    fixedTestQuestionResultVos.forEach(k -> k.setQuestionResultVo(collect.get(k.getSmallQuestionId())));
                    return fixedTestQuestionResultVos;
                }
                return new ArrayList<>();
            }));
        }
        return null;
    }

    @Override
    public void delete(List<Integer> ids) {
        removeByIds(ids);
    }

    @Override
    public void update(FixedTestQuestionUpdateVo fixedTestQuestionUpdateVo) {
        FixedTestQuestion fixedTestQuestion = BeanCopyUtils.copyBean(fixedTestQuestionUpdateVo, FixedTestQuestion.class);
        updateById(fixedTestQuestion);
    }

    @Override
    public List<FixedTestQuestion> getByExaminationIds(List<Integer> ids) {
        LambdaQueryWrapper<FixedTestQuestion> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(FixedTestQuestion::getExaminationId,ids);
        return list(wrapper);
    }

    @Override
    public void deleteBySmallQuestionIds(List<Integer> ids) {
        LambdaQueryWrapper<FixedTestQuestion> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(FixedTestQuestion::getSmallQuestionId,ids);
        remove(wrapper);
    }

    @Override
    public void deleteByExaminationIds(List<Integer> ids) {
        LambdaQueryWrapper<FixedTestQuestion> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(FixedTestQuestion::getExaminationId, ids);
        remove(wrapper);
    }
}
